<?php
class SysConfigAction extends CommonAction {
	private $role_arr = array(
			'0' => '管理员',
			'1' => '订货人',
			'2' => '查询人员',
	);
	
	function _initialize(){
		parent::_initialize();
	}
	
    function index(){
    	$model = D('admin_user');
        $voList = $model->order("`user_id` asc")->select();
        //模板赋值显示
        
        $sys_model = D('sys_config');
        $cfg_list = $sys_model->order("`sort_id` asc")->select();
        
        
        $this->assign('role_arr', $this->role_arr);
        $this->assign('list', $voList);
        $this->assign('cfg_list', $cfg_list);
        cookie('_currentUrl_', __SELF__);
    	$this->display();
    }
    
    function cfg_update(){
    	if($this->isPost()){
    		$sys_model = D('sys_config');
    		
    		foreach ($_POST as $k => $v){
    			$sys_model->where("cfg_key = '$k'")->save(array('cfg_value'=>$v));
    		}
    		
    		$this->success('配置保存成功！', cookie('_currentUrl_'));
    	}
    }
    
    function reset_pass(){
    	$id = $this->_param('id', 'trim');
    	$model = D('admin_user');
    	$new_pass = $this->_generate_password(6);
    	$data = array('user_pass' => md5($new_pass));
    	$result = $model->where("user_id='$id'")->save($data);
    	
    	if($result)
    		$this->success($new_pass);
    	else
    		$this->error('处理失败！'.$model->_sql());
    }
    
    function change_role(){
    	$id = $this->_param('id', 'trim');
    	
    	if($id == '0')
    		$this->error('超级管理员不允许变更角色！');
    	
    	$role_id = $this->_param('role_id', 'trim');
    	$model = D('admin_user');
    	$data = array('user_role' => $role_id);
    	$result = $model->where("user_id='$id'")->save($data);
    	
    	if($result)
    		$this->success('处理成功');
    	else
    		$this->error('处理失败！'.$model->_sql());
    }
    
    function _generate_password( $length = 8 ) {
    	// 密码字符集，可任意添加你需要的字符
    	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
    	$chars = '0123456789';
    
    	$password = '';
    	for ( $i = 0; $i < $length; $i++ ) {
    		// 这里提供两种字符获取方式
    		// 第一种是使用 substr 截取$chars中的任意一位字符；
    		// 第二种是取字符数组 $chars 的任意元素
    		// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    		$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    	}
    
    	return $password;
    }
    
    function admin_user_add(){
    	if($this->isPost()){
    		$role_id = $this->_post('role_id', 'trim');
    		
    		if(!isset($this->role_arr[$role_id]))
    			$this->error('角色错误！');
    		
    		$data = array(
    				'user_name'=>$this->_post('user_name', 'trim'),
    				'user_pass'=>$this->_post('user_pass', 'trim,md5'),
    				'true_name'=>$this->_post('true_name', 'trim'),
    				'user_role'=>$role_id,
    		);
    		$flag = D('admin_user')->add($data);
    		if($flag)
    			$this->success('添加成功', cookie('_currentUrl_'));
    		else
    			$this->error('添加失败！');
    	}
    	$this->assign('role_arr', $this->role_arr);
    	$this->display();
    }
}